package com.yida.admin;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yida.bean.blog;
import com.yida.bean.blogtype;
import com.yida.bean.tag;
import com.yida.service.serverblog;

@Controller
@RequestMapping("/admin")
public class blogbj {

	@Autowired
	public serverblog service;
	
	@RequestMapping("/blogs/bj")
	public String bj(Model mode, Integer id) {
		//初始化分类
        List<blogtype> types = service.selectblogtypeservice();
	    mode.addAttribute("types", types);
	    //初始化标签
	    List<tag> tags = service.selecttagservice();
	    
	    mode.addAttribute("tags", tags);
		blog blog = service.selectBlogservice(id);
		blog.init();
		
		mode.addAttribute("blog", blog);
		
		
		return "admin/blogs-bj";
	}
	
    @RequestMapping("/blogs/bj/tx")
	public String tx(blog blog,Integer type,int[] tagIds,RedirectAttributes attributes) {
    	java.sql.Date date = new java.sql.Date(new Date().getTime());
		blog.setReleaseDate(date);
		boolean pd = service.updateblog(blog, type, tagIds);
		if (pd) {
			 attributes.addFlashAttribute("message","修改成功!");
		}else {			
			 attributes.addFlashAttribute("message","修改失败!");
		}
		 return "redirect:/admin/blogs";
	}
    
}

